#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include <thread>

#include "DownLoadCenter.h"
#include "ShowLayer.h"
#include "Func_ios.h"
#include "MyObj.h"

USING_NS_CC;

using namespace cocostudio::timeline;

Scene* HelloWorld::createScene()
{
    auto scene = Scene::create();
    auto layer = HelloWorld::create();
    scene->addChild(layer);
    layer->scheduleOnce([=](float f){
        layer->delayShowUI();
    }, 2, "delayShowUI");
    return scene;
}

bool HelloWorld::init()
{
    if ( !Layer::init() ) return false;
    return true;
}




void HelloWorld::delayShowUI()
{

//    std::thread t1( &HelloWorld::testThread, this );
//    t1.detach();
    Sprite *sp = Sprite::create("img/love.png");
    this->addChild(sp);
    MoveTo *mov = MoveTo::create(10, Point(1280,640) );
    Repeat *rep = Repeat::create(mov, 10000);
    sp->runAction(rep);
//    return;


    
    log("正式开始 delay 方法内事件");
    DownLoadCenter *t = DownLoadCenter::getInstance();
    t->setWebName(WEB_69);
    for(int i=600;i<12660;i++)
    {
        t->addWebTasK( __String::createWithFormat("http://www.69bobo.com/art-detail-id-%d.html", i)->getCString() );
    }
    DownLoadCenter::getInstance()->startRun();
    return;


    // ********
    auto lay = ShowLayer::create();
    lay->show();
    DownLoadCenter::getInstance()->setOneTaskFinishCallBack([=](int taskId, std::string &taskURL,OneTaskInfo &info){
        lay->scheduleOnce([=](float f){
            lay->reloadData();
        }, 0.5, "delayReoloadData");
    });
    DownLoadCenter::getInstance()->setUpdateEvent([=](int taskId, int downTask){
        lay->scheduleOnce([=](float f){
            lay->reloadData();
        }, 0.5, "delayImgReoloadData");
    });

    Node *nod = Node::create();
    Director::getInstance()->getRunningScene()->addChild(nod);

    auto eve1 = CallFunc::create([](){});
    Sprite *cc;
    DelayTime::create(0.1);
    Sequence::create(nullptr, NULL);

}





void HelloWorld::testThread()
{
    MyDictionary *allDic = MyDictionary::create();
    for(int i=1;i<20000;i++)    //  [600 ~ 12660]
    {
        log("%d", i);
        MyDictionary *oneInfo = MyDictionary::create();
        MyString *taskURL = MyString::createWithFormat("http://www.69bobo.com/art-detail-id-%d.html", i);
        MyString *url = MyString::createWithFormat("%s", taskURL->getCString());
        MyString *title = MyString::createWithFormat("%s", "完成" );
        MyString *imgNum = MyString::createWithFormat("%d", 0 );
        MyString *taskId = MyString::createWithFormat("%d", i );
        MyString *state = MyString::createWithFormat("%s", TASK_STATE_wait );
        oneInfo->setObject( url, INFO_ONE_WebURL);
        oneInfo->setObject( title,  INFO_ONE_TITLE);
        oneInfo->setObject( imgNum, INFO_ONE_IMG_NUMBER );
        oneInfo->setObject( taskId, INFO_ONE_ID);
        oneInfo->setObject( state, INFO_ONE_STATE);
        url->release();
        title->release();
        imgNum->release();
        taskId->release();
        state->release();
        taskURL->release();
        //----
        MyArray *imgs = MyArray::create();
        oneInfo->setObject(imgs, INFO_ONE_IMG_ARY);
        imgs->release();
        for(int j=0;j<30;j++)
        {
            MyString *str = MyString::createWithFormat("http://www.69bobo.com/art-detail-id-%d.html", j);;
            imgs->addObject(str);
            str->release();
        }
        // 只能通过ID寻找，同时可以通过 url 和 taskID 寻找一条信息。
        allDic->setObject(oneInfo, MyString::createWithFormat("%d", i)->getCString() );
        oneInfo->release();
    }
    m_allDic = allDic;
    ////////////////////////////////////
    Func_ios::Log(" save start ");
    std::string testPath = FileUtils::getInstance()->getWritablePath() + "test.plist";
    if(m_allDic->writeToFile(testPath.c_str()) )
        log(" plist [%s] 保存成功", testPath.c_str() );
    else
        log(" plist 保存失败");
    Func_ios::Log(" end  add task");


    for(int i=0;i<5000;i++)
    {
        MyDictionary *mDic = MyDictionary::createWithContentsOfFile(testPath.c_str());
        MyDictionary *oneInfo = (MyDictionary *)mDic->objectForKey("553");
        MyString *str = (MyString *)oneInfo->objectForKey( INFO_ONE_WebURL );
        log(" %d 次取文件: [%d] 内容: %s", i, mDic->count(), str->getCString() );
        mDic->release();
    }


    if( m_Func )
        m_Func(1,2);
    m_Func = nullptr;
}







